/**
 * 具体的资源池信息
 */
$(function () {
    // 获取url参数
    var urlParams = getQueryVariable();
    var userInfo = getUserInfo();
    initMore();

    function initMore(){
        var token = urlParams["token"];
        if(token){
            init(function(){
                userInfo = getUserInfo();
                rebuildHtml();
            });
        } else {
            rebuildHtml();
        }
    }

    // 对接后显示
    var checkArray = ["fzphone","fzemail","fzphone2","fzemail2"];
    var fontSize= "16px"


    /**
     * 重构页面
     */
    function rebuildHtml() {
        var id = urlParams["id"];
        getInfo(id);
    }

    // 获取参数
    function getQueryVariable() {
        var url = window.location.search; //获取url中"?"符后的字串
        var paramsJson = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                paramsJson[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return paramsJson;
    }

    // 获得数据接口
    function getInfo(id) {
        let params = {
            url: "/index/lib/Harvest/" + id,
            headers: {token: userInfo==null?null:userInfo.token},
            sCallback: function (data) {
                successCallback(data);
                // checkstatus ！= -1的 均不可以进行对接
                var checkStatus = data["checkStatus"];
                if (checkStatus != -1) {
                    hideMeeting();
                }
            },
            eCallback: function (res) {
                if (res.code == 10004) {
                    successCallback(res.data);
                    // 需要重新登陆
                    $('#loginModal').modal('show');
                    window.location.href = indexUrl;
                }
            }
        }
        requset(params);
    }

    // 详细信息成功的回调
    function successCallback(data) {
        var checkStatus = data["checkStatus"];
        $("#page-header").text(data["resName"]);
        $("input").each(function () {
            var id = $(this).attr("id");
            $.each(data, function (index, data) {
                if (id == index) {
                    // 这个id存在并且对接之后checkstatus == 1才显示数据
                    if(checkArray.indexOf(id) != -1 && checkStatus == 1){
                        $("#"+index).val(data);
                    } else if(checkArray.indexOf(id) != -1 && checkStatus != 1) {
                        $("#"+index).val("对接后可查看");
                    } else {
                        $("#"+index).val(data);
                    }
                    $("#"+index).css("font-size",fontSize);
                }
            })
        });
        $("textarea").each(function () {
            var id = $(this).attr("id");
            $.each(data, function (index, data) {
                if (id == index) {
                    $("#" + index).val(data);
                }
                $("#"+index).css("font-size",fontSize);
            })
        });

        var patentsList = data["patents"];
        var parseJson = JSON.parse(patentsList);
        var htmlCode = "<table border='1' width='100%' ><tr>" +
            "                <th>专利名称</th>" +
            "                <th>专利类型</th>" +
            "                <th>专利号</th>" +
            "                <th>专利权人</th>" +
            "                <th>有效期</th>" +
            "                </tr>";
        if(parseJson != null && parseJson.length >0){
            $.each(parseJson, function (index, value) {
                var projecType = value["projecType"];
                var projectname = value["projectname"];
                var projectNum = value["projectNum"];
                var projectPer = value["projectPer"];
                var projectTime = value["projectTime"];
                if (projectTime == "未填") {
                    projectTime = "";
                }
                // htmlCode += " <textarea class='form-control' rows='6'>专利名称： " + projectname + "\r\n" + "专利类型： " + projecType
                //     + "\r\n" + "专利号：  " + projectNum + "\r\n" + "专利权人：  " + projectPer + "\r\n" + "有效期：    " + projectTime + "</textarea>";
                htmlCode += "  <tr><td>"+projectname+"</td><td>"+projecType+"</td><td>"+projectNum+"</td><td>"+projectPer+"</td><td>"+projectTime+"</td></tr>";
            })
        } else {
            htmlCode += "  <tr><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td></tr>";
        }

        htmlCode += "</table>"
        $("#patents").html(htmlCode);
    }


    // 发起对接
    $("#meeting").on("click", function () {
        var isLogin = checkLogin();
        if (isLogin) {
            resetFormData();
            $('#myModal').modal('show');
        } else {
            $("#login_btn").click();
        }

    })

    function resetFormData() {
        var storage=window.sessionStorage;
        var userInfoLocal = {};
        if (storage.userInfo) {
            userInfoLocal = JSON.parse(storage.userInfo);
            $("#fcp").val(userInfoLocal.cellphone);
            $("#fcm").val(userInfoLocal.email);
            if(userInfoLocal.userType  == 1) {
                $("#fcn").val(userInfoLocal.name);
            } else {
                $("#fca").val(userInfoLocal.name);
            }
        } else {
            $("#fcn").val("");
            $("#fcp").val("");
            $("#fcm").val("");
            $("#fca").val("");
        }
        $("#appNote").val("");
    }

    // 确认对接
    $("#confirm").on("click", function () {
        let params = {
            type: "post",
            contentType: "application/x-www-form-urlencoded",
            headers: {token: userInfo.token},
            data: getFormData(),
            url: "/index/meeting/apply",
            sCallback: function (data) {
                saveSuccessCallback(data)
            },
            eCallback: function (res) {
                errorSuccessCallback(res)
            }
        }
        requset(params);
    })

    function getFormData() {
        var appId = urlParams["id"];
        var fcn = $("#fcn").val();
        var fcp = $("#fcp").val();
        var fcm = $("#fcm").val();
        var fca = $("#fca").val();
        var appNote = $("#appNote").val();
        return {appId: appId, typeId: 2, fcn: fcn, fcp: fcp, fcm: fcm, fca: fca, appNote: appNote}
    }

    function saveSuccessCallback(data) {
        $("#result_content").text('操作成功！');
        $("#resultModal").modal('show');
        $("#myModal").modal('hide');
    }

    function errorSuccessCallback(res) {
        $("#result_content").text(res.message);
        $("#resultModal").modal('show');
    }

    $("#result_btn").on("click", function () {
        $("#resultModal").modal('hide');
    })
    // 取消对接
    $("#cancel").on("click", function () {
        $("#myModal").modal('hide');
    })

    // 取消重新登陆
    $("#cancelLogin").on("click", function () {
        $('#loginModal').modal('hide');
    })
    // 确认重新登陆
    $("#confirmLogin").on("click", function () {
        $("#login_btn").click();
    })

});


